IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Address_Customer]') AND parent_object_id = OBJECT_ID(N'[Address]'))
ALTER TABLE [Address] DROP CONSTRAINT [FK_Address_Customer]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Customer_Customer]') AND parent_object_id = OBJECT_ID(N'[Customer]'))
ALTER TABLE [Customer] DROP CONSTRAINT [FK_Customer_Customer]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Customer_User]') AND parent_object_id = OBJECT_ID(N'[Customer]'))
ALTER TABLE [Customer] DROP CONSTRAINT [FK_Customer_User]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_User_Role_User]') AND parent_object_id = OBJECT_ID(N'[User_Role]'))
ALTER TABLE [User_Role] DROP CONSTRAINT [FK_User_Role_User]
GO


TRUNCATE TABLE Address
TRUNCATE TABLE Customer
TRUNCATE TABLE [User]

GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Customer_Customer]') AND parent_object_id = OBJECT_ID(N'[Customer]'))
ALTER TABLE [Customer]  WITH CHECK ADD  CONSTRAINT [FK_Customer_Customer] FOREIGN KEY([ParentCustomerID])
REFERENCES [Customer] ([CustomerID])
GO
ALTER TABLE [Customer] CHECK CONSTRAINT [FK_Customer_Customer]
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Customer_User]') AND parent_object_id = OBJECT_ID(N'[Customer]'))
ALTER TABLE [Customer]  WITH CHECK ADD  CONSTRAINT [FK_Customer_User] FOREIGN KEY([UserID])
REFERENCES [User] ([UserID])
GO
ALTER TABLE [Customer] CHECK CONSTRAINT [FK_Customer_User]
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Address_Customer]') AND parent_object_id = OBJECT_ID(N'[Address]'))
ALTER TABLE [Address]  WITH CHECK ADD  CONSTRAINT [FK_Address_Customer] FOREIGN KEY([CustomerID])
REFERENCES [Customer] ([CustomerID])
GO
ALTER TABLE [Address] CHECK CONSTRAINT [FK_Address_Customer]
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_User_Role_User]') AND parent_object_id = OBJECT_ID(N'[User_Role]'))
ALTER TABLE [User_Role]  WITH CHECK ADD  CONSTRAINT [FK_User_Role_User] FOREIGN KEY([UserID])
REFERENCES [User] ([UserID])
GO
ALTER TABLE [User_Role] CHECK CONSTRAINT [FK_User_Role_User]
GO


